import sys

f = open('p059_cipher.txt', 'r')

orda = ord('a')
ordz = ord('z')

text = []
for line in f:
    l = [int(s) for s in line.split(',')]
    text.extend(l)

theCount = 1
best = [orda, orda, orda]

for a in xrange(orda, ordz + 1):
    for b in xrange(orda, ordz + 1):
        for c in xrange(orda, ordz + 1):
            key = [a, b, c]
            t = []
            n = len(text)
            for i in xrange(n):
                t.append(text[i] ^ key[i % 3])
            tt = [chr(num) for num in t]
            tt = ''.join(tt)
            count = tt.count('the') + tt.count('The')
            if count > theCount:
                theCount = count
                best = key
                print tt
                print key, sum(t)





